package org.films.domain;

import java.lang.Integer;
import java.lang.Long;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Version;
import org.films.domain.Consumer;
import org.springframework.transaction.annotation.Transactional;



privileged aspect Consumer_Roo_Entity {
	
	private static String INSERT_AUTHORITY="INSERT INTO authorities (USERNAME, AUTHORITY) VALUES (?, ?)";
	private static String ROLE_USER = "ROLE_USER";
	private static String MOST_ACTIVE_USER_QUERY = "select *" +
													"from Comment c join users u" +
													"ON c.consumer=u.id" +
													"group by u.username" +
													"having(count(c.id)=(select max(cnt) as maxComment from" +
													"(select count(c.consumer) as cnt from Comment c group by c.consumer) o))";
    
    @PersistenceContext    
    transient EntityManager Consumer.entityManager;    
    
    @Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long Consumer.id;    
    
    @Version    
    @Column(name = "version")    
    private Integer Consumer.version;    
    
    public Long Consumer.getId() {    
        return this.id;        
    }    
    
    public void Consumer.setId(Long id) {    
        this.id = id;        
    }    
    
    public Integer Consumer.getVersion() {    
        return this.version;        
    }    
    
    public void Consumer.setVersion(Integer version) {    
        this.version = version;        
    }    
    
    @Transactional    
    public void Consumer.persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();
        Long id = this.getId();
        this.entityManager.persist(this);
        this.entityManager.flush();
        if (isNewUser(id)) {
        	this.entityManager.createNativeQuery(INSERT_AUTHORITY)
        					 .setParameter(1, this.getUsername())
        					 .setParameter(2, ROLE_USER)
        					 .executeUpdate();
        }                
    }

	private static boolean isNewUser(Long id) {
		return id == null;
	}    
    
    @Transactional    
    public void Consumer.remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();
        Query query = entityManager.createNativeQuery("delete from authorities where username=?");
        if (this.entityManager.contains(this)) {        	
        	query.setParameter(1, getUsername());
            this.entityManager.remove(this);            
            query.executeUpdate();
        } else {        
            Consumer attached = this.entityManager.find(Consumer.class, this.id);
            query.setParameter(1, attached.getUsername());
            query.executeUpdate();
            this.entityManager.remove(attached);            
        }        
    }    
    
    @Transactional    
    public void Consumer.flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }    
    
    @Transactional    
    public void Consumer.merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        Consumer merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }    
    
    public static final EntityManager Consumer.entityManager() {    
        EntityManager em = new Consumer().entityManager;        
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }    
    
    public static long Consumer.countConsumers() {    
        return (Long) entityManager().createQuery("select count(o) from Consumer o").getSingleResult();        
    }    
    
    public static List<Consumer> Consumer.findAllConsumers() {    
        return entityManager().createQuery("select o from Consumer o").getResultList();        
    }    
    
    public static Consumer Consumer.findConsumer(Long id) {    
        if (isNewUser(id)) throw new IllegalArgumentException("An identifier is required to retrieve an instance of Consumer");        
        return entityManager().find(Consumer.class, id);        
    }    
    
    public static List<Consumer> Consumer.findConsumerEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from Consumer o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }    
    
    public static List<Consumer> Consumer.findMostActiveUsers() {    	
    	return (List<Consumer>)entityManager().createQuery("select u " +
    												"from Consumer u " +
    												"where u.comments.size = (select MAX(u2.comments.size) from Consumer u2)")
    									.getResultList();    	
    }
    
    public static Consumer Consumer.findConsumerByName(String name) {    
        if (name == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of Consumer");        
        return (Consumer)entityManager().createQuery("select o from Consumer o where o.username=:username")
        					  .setParameter("username", name)
        					  .getSingleResult();        
    } 
    
}
